$(document).ready(function() {
	// $('.buttons-row .btn-country').picker({
	// 	toolbarTemplate: '<header class="bar bar-nav">\
 //  			<button class="button button-link pull-left">按钮</button>\
 //  			<button class="button button-link pull-right close-picker">确定</button>\
 //  			<h1 class="title">标题</h1>\
 //  		</header>',	
 //  		cols: [{
 //  			textAlign: 'center',
 //  			values: ['德玛西亚', '守望之海']
 //  		}]
	// });
	

	renderList();

	$(document).on('click', '.j-look', function(e) {
		var id = $(e.currentTarget).data('id');
		fetchMax(id, function(amount) {
			$('body').append($.confirm('您今天还有' + amount + '次机会', function(){
				window.location = './travel_info.html?openid = ' + id;
			}));
		});
	})

	$('.j-self').on('click', function() {
		window.location = '../travel_insert.html';
	})

	function fetchMax(id, callback) {
		callback(3)
		// $.ajax({
		// 	type: 'GET',
		// 	url: 'https://dmpb.com.cn/api/travel/remainViewNum?openid=' + id,
		// 	success: function(res) {
		// 		callback && callback(res.content);
		// 	}
		// });
	}

	function renderList() {
		fetchList(function(list) {
			var div = document.createElement('div');
			var content = list.reduce(function(memo, item) {
				return memo + getItemHtml(item);
			}, '');
			$('.content .list').html(content);
		});
	}

	function fetchList(callback) {
		$.ajax({
			type: 'GET',
			url: 'https://dmpb.com.cn/api/travel/list',
			success: function(res) {
				callback && callback(res.content);
			}
		});
	}

	function getItemHtml(data) {
		return [
			'<div class="content-item">',
				'<a href="javascript:;" class="j-look" data-id="' + data.openid + '">',
					'<div>',
						'<span class="name">' + data.name + '</span>',
						'<span class="sex">性别</span>',
						'<em>|</em>',
						'<span class="age">80后</span>',
						'<em>|</em>',
						'<span class="paty">' + data.supportTeams.join(',') + '</span>',
						'<em>|</em>',
						'<span class="amount">' + data.peerNum + '人</span>',
					'</div>',
					'<div>',
						'<span class="watch">观赛信息:</span>',
						'<span class="ticket">' + data.matches.join(',') + '</span>',
					'</div>',
					'<div>',
						'<span class="time">' + data.startDate + '~' + data.returnDate + '</span>',
						'<em>|</em>',
						'<span class="position">' + data.toCity + '</span>',
					'</div>',
					'<div>',
						'<span class="intro">简介:</span>',
						'<span class="desc">' + data.description + '</span>',
					'</div>',
				'</a>',
			'</div>'
		].join('')
	}

})
